<template>
  <div class="app-main-container">
    <div class="warnningTip">
      <i class="el-icon-warning-outline" />
      {{ translate("yhqhyxzcdj") }}
    </div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    />
    <el-tabs v-model="marketingActiveName" @tab-click="handleClick">
      <el-tab-pane :label="translate('disPolicy')" name="2">
        <div class="doumee-table-buttons">
          <el-button
            v-if="meta.indexOf('MarketAdd') > -1"
            v-preventReClick
            type="primary"
            @click="toVipCardAdd('2')"
          >{{ translate("add") }}</el-button>
        </div>
        <el-table
          ref="table"
          v-loading="listLoading"
          :data="list"
          element-loading-text="Loading"
          border
          fit
          :header-row-class-name="'table-header'"
          class="doumee-element-table"
        >
          <el-table-column
            align="center"
            :label="translate('policyName')"
            prop="marketingName"
            min-width="200"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <span
                class="primaryColor pointer"
                @click="detailClick(scope.row)"
              >{{ scope.row.marketingName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('policyRules')"
            prop="useDes"
            min-width="200"
            show-overflow-tooltip
          >
            <!-- <template v-slot="scope">
              <span>订单实付金额满{{ scope.row.amount }}减{{ scope.row.discountNum }}</span>
            </template> -->
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('orderType')"
            prop="orderTypeInfo"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('useCount')"
            prop="useNum"
            min-width="80"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('applyVenue')"
            prop="stadiumInfo"
            min-width="240"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('applyCycle')"
            prop="useTime"
            min-width="120"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <div>{{ scope.row.useDateStart }}</div>
              <div>{{ scope.row.useDateEnd }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('applySection')"
            prop="useTime"
            min-width="190"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('payMethod')"
            prop="payMethodInfo"
            min-width="240"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('status')"
            min-width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.marketingStatus"
                active-value="1"
                inactive-value="0"
                @change="statusChang(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="translate('handle')"
            :width="$i18n.locale === 'ZH'? 120 : 180"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="flex-cc">
                <el-button
                  v-if="
                    scope.row.marketingStatus == '1' &&
                      meta.indexOf('MarketingEdit') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="copy(scope.row)"
                >{{ translate("copy") }}</el-button>
                <el-button
                  v-if="
                    scope.row.marketingStatus == '0' &&
                      meta.indexOf('MarketingEdit') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="edit(scope.row)"
                >{{ translate("edit") }}</el-button>
                <el-button
                  v-if="
                    scope.row.marketingStatus == '0' &&
                      meta.indexOf('MarketingDel') > -1
                  "
                  v-preventReClick
                  type="text"
                  style="color: red"
                  @click="deleteItem(scope.row)"
                >{{ translate("del") }}</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane :label="translate('manjianPolicy')" name="0">
        <div class="doumee-table-buttons">
          <el-button
            v-if="meta.indexOf('MarketAdd') > -1"
            v-preventReClick
            type="primary"
            @click="toVipCardAdd('0')"
          >{{ translate("add") }}</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="list"
          element-loading-text="Loading"
          border
          fit
          :header-row-class-name="'table-header'"
          class="doumee-element-table"
        >
          <el-table-column
            align="center"
            :label="translate('policyName')"
            prop="marketingName"
            min-width="200"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <span
                class="primaryColor pointer"
                @click="detailClick(scope.row)"
              >{{ scope.row.marketingName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('policyRules')"
            prop="useDes"
            min-width="200"
            show-overflow-tooltip
          >
            <!-- <template v-slot="scope">
              <span>订单实付金额满{{ scope.row.amount }}减{{ scope.row.discountNum }}</span>
            </template> -->
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('orderType')"
            prop="orderTypeInfo"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('useCount')"
            prop="useNum"
            min-width="80"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('applyVenue')"
            prop="stadiumInfo"
            min-width="240"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('applyCycle')"
            prop="useTime"
            min-width="120"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <div>{{ scope.row.useDateStart }}</div>
              <div>{{ scope.row.useDateEnd }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('applySection')"
            prop="useTime"
            min-width="190"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('payMethod')"
            prop="payMethodInfo"
            min-width="240"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('status')"
            min-width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.marketingStatus"
                active-value="1"
                inactive-value="0"
                @change="statusChang(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="translate('handle')"
            min-width="120"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="flex-cc">
                <el-button
                  v-if="
                    scope.row.marketingStatus == '1' &&
                      meta.indexOf('MarketingEdit') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="copy(scope.row)"
                >{{ translate("copy") }}</el-button>
                <el-button
                  v-if="
                    scope.row.marketingStatus == '0' &&
                      meta.indexOf('MarketingEdit') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="edit(scope.row)"
                >{{ translate("edit") }}</el-button>
                <el-button
                  v-if="
                    scope.row.marketingStatus == '0' &&
                      meta.indexOf('MarketingDel') > -1
                  "
                  v-preventReClick
                  type="text"
                  style="color: red"
                  @click="deleteItem(scope.row)"
                >{{ translate("del") }}</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane :label="translate('manzengPolicy')" name="1">
        <div class="doumee-table-buttons">
          <el-button
            v-preventReClick
            type="primary"
            @click="toVipCardAdd('1')"
          >{{ translate("add") }}</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="list"
          element-loading-text="Loading"
          border
          fit
          :header-row-class-name="'table-header'"
          class="doumee-element-table"
        >
          <el-table-column
            align="center"
            :label="translate('policyName')"
            prop="marketingName"
            min-width="200"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <span
                class="primaryColor pointer"
                @click="detailClick(scope.row)"
              >{{ scope.row.marketingName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('orderType')"
            prop="orderTypeInfo"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('useCount')"
            prop="useNum"
            min-width="80"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('zengsongType')"
            min-width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{
                scope.row.giveType == "0" ? translate("jine") : translate("goods")
              }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('policyRules')"
            prop="useDes"
            min-width="250"
            show-overflow-tooltip
          >
            <!-- <template v-slot="scope">
              <span v-if="scope.row.giveType == '0'">订单实付金额满{{ scope.row.amount }}赠送{{ scope.row.giveItems }}</span>
              <span v-else>订单实付金额满{{ scope.row.amount }}赠送{{ scope.row.giveItems }} * {{ scope.row.giveNum }}</span>
            </template> -->
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('applyVenue')"
            prop="stadiumInfo"
            min-width="240"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('applyCycle')"
            prop="useTime"
            min-width="120"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <div>{{ scope.row.useDateStart }}</div>
              <div>{{ scope.row.useDateEnd }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="translate('applySection')"
            prop="useTime"
            min-width="190"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('payMethod')"
            prop="payMethodInfo"
            min-width="240"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="translate('status')"
            min-width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.marketingStatus"
                active-value="1"
                inactive-value="0"
                @change="statusChang(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="translate('handle')"
            min-width="120"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="flex-cc">
                <el-button
                  v-if="scope.row.marketingStatus == '0'"
                  v-preventReClick
                  type="text"
                  @click="edit(scope.row)"
                >{{ translate("edit") }}</el-button>
                <el-button
                  v-if="scope.row.marketingStatus == '1'"
                  v-preventReClick
                  type="text"
                  @click="copy(scope.row)"
                >{{ translate("copy") }}</el-button>
                <el-button
                  v-if="scope.row.marketingStatus == '0'"
                  v-preventReClick
                  type="text"
                  style="color: red"
                  @click="deleteItem(scope.row)"
                >{{ translate("del") }}</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <Pagination
        :total="totalCount"
        :page="pagination.page"
        @current-change="getList"
        @SizeChange="handleSizeChange"
      />
    </el-tabs>
    <MarketAdd
      v-if="isShowMarketAdd"
      ref="MarketAddRef"
      @success="MarketAddScs"
      @close="isShowMarketAdd = false"
    />
    <MarkeDEdit
      v-if="isShowMarketDEdit"
      ref="MarketDEditRef"
      @success="MarketAddScs"
      @close="isShowMarketDEdit = false"
    />
    <MarketDetail ref="MarketDetailRef" :meta="meta" />
  </div>
</template>

<script>
import { marketList, marketEnable, venueList, allConfigList, marketDel, orderTypeList } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import MarketAdd from './marketAdd/index.vue'
import MarkeDEdit from './discount'
import MarketDetail from './detail.vue'
import { queryFormConfig } from './config'
export default {
  name: 'Marketing',
  components: {
    Pagination,
    MarketAdd,
    MarkeDEdit,
    MarketDetail
  },
  data() {
    return {
      isShowMarketAdd: false,
      isShowMarketDEdit: false,
      queryFormConfig,
      orderType: [],
      orderTypeb: [],
      list: [],
      meta: [],
      listLoading: true,
      pagination: {
        page: 1,
        rows: 10
      },
      totalCount: 10,
      marketingActiveName: '2',
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getPayTypeList()
    this.getOrderTypeList()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    detailClick(row) {
      const { marketingId } = row
      // this.$refs.MarketDetailRef.detail = { ... row }
      this.$refs.MarketDetailRef.getMarketDetail(marketingId, row)
      this.$refs.MarketDetailRef.isShowDrawer = true
    },
    MarketAddScs() {
      this.isShowMarketAdd = false
      this.getList()
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    // 获取订单类型列表
    getOrderTypeList() {
      // orderTypeList({ param: { type: 'ORDER_TYPE_MARKETING_REDUCE' }}).then(
      orderTypeList({ param: { type: 'ORDER_TYPE_MARKETING_GIVE' }}).then(
        (res) => {
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[1].options = arr
          this.orderType = arr
        }
      )
      orderTypeList({ param: { type: 'ORDER_TYPE_MARKETING_GIVE' }}).then(
        (res) => {
          if (res.errorCode !== '000000') return
          const arrb = res.recordList
          arrb.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.orderTypeb = arrb
        }
      )
    },
    toVipCardAdd(type) {
      if (type === '2') {
        this.isShowMarketDEdit = true
        this.$nextTick(() => {
          this.$refs.MarketDEditRef.isShowDrawer = true
          this.$refs.MarketDEditRef.param.marketingType = type
        })
        return
      }
      this.isShowMarketAdd = true
      this.$nextTick(() => {
        this.$refs.MarketAddRef.isShowDrawer = true
        this.$refs.MarketAddRef.param.marketingType = type
      })
      // this.$router.push({ name: 'MarketAdd', query: { type: type }})
    },
    edit(item) {
      if (this.marketingActiveName === '2') {
        this.isShowMarketDEdit = true
        this.$nextTick(() => {
          this.$refs.MarketDEditRef.copName = this.translate('policyEdit')
          this.$refs.MarketDEditRef.isShowDrawer = true
          this.$refs.MarketDEditRef.id = item.marketingId
          this.$refs.MarketDEditRef.getMarketDetailInfo(item.marketingId)
        })
        return
      }
      this.isShowMarketAdd = true
      this.$nextTick(() => {
        this.$refs.MarketAddRef.copName = this.translate('policyEdit')
        this.$refs.MarketAddRef.isShowDrawer = true
        this.$refs.MarketAddRef.id = item.marketingId
        this.$refs.MarketAddRef.getMarketDetailInfo(item.marketingId)
      })
      // this.$router.push({ name: 'MarketAdd', query: { id: item.marketingId }})
    },
    copy(item) {
      if (this.marketingActiveName === '2') {
        this.isShowMarketDEdit = true
        this.$nextTick(() => {
          this.$refs.MarketDEditRef.copName = this.translate('policyAdd')
          this.$refs.MarketDEditRef.isShowDrawer = true
          this.$refs.MarketDEditRef.id = ''
          this.$refs.MarketDEditRef.getMarketDetailInfo(item.marketingId)
        })
        return
      }
      this.isShowMarketAdd = true
      this.$nextTick(() => {
        this.$refs.MarketAddRef.isShowDrawer = true
        this.$refs.MarketAddRef.getMarketDetailInfo(item.marketingId)
      })
    },
    handleClick(tab, event) {
      const obj = { ... this.queryFormConfig }
      if (this.marketingActiveName === '0') {
        obj.formItems[1].options = this.orderTypeb
      } else {
        obj.formItems[1].options = this.orderType
      }
      this.queryFormConfig = obj
      this.getList(1)
    },
    statusChang(item) {
      marketEnable({
        param: {
          id: item.marketingId,
          status: item.marketingStatus
        }
      }).then(res => {
        if (res.errorCode !== '000000') return
        const message = item.marketingStatus === '1' ? this.translate('statusSnableScs') : this.translate('statusStopScs')
        this.$message.success(message)
        this.getList()
      })
    },
    deleteItem(item) {
      this.$alert(this.translate('policyDelPlace'), this.translate('warning'), {
        confirmButtonText: this.translate('qd'),
        callback: action => {
          if (action === 'confirm') {
            console.log(item.marketingId)
            marketDel({
              param: {
                id: item.marketingId
              }
            }).then(res => {
              if (res.errorCode !== '000000') return
              this.getList()
            })
          }
        }
      })
    },
    // 清空
    clear() {
      this.filters = {}
      this.getList(1)
    },
    getVenueList() {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[2].options = arr
        })
        .catch(() => { })
    },
    // 获取支付方式列表
    getPayTypeList() {
      allConfigList({ param: { type: '2', code: 'PAY_METHOD_MARKETING' }}).then((res) => {
        if (res.errorCode !== '000000') return
        const arr = res.recordList || []
        arr.forEach(e => {
          e.label = e.name
        })
        this.queryFormConfig.formItems[3].options = arr
      })
    },
    getList(page) {
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      marketList({
        pagination,
        userId: sessionStorage.getItem('userId'),
        param: { ...filters, type: this.marketingActiveName }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
